package org.jackysoft.service.impl;

import java.util.Collection;
import java.util.List;

import org.jackysoft.entity.MonthPlanningItem;
import org.jackysoft.entity.PlanItemModifiedRecord;
import org.jackysoft.service.AbstractServiceImpl;
import org.jackysoft.service.PlanItemModifiedRecordService;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;


@Service
@Transactional
public class PlanItemModifiedRecordServiceImpl extends
AbstractServiceImpl<String, PlanItemModifiedRecord> implements
		PlanItemModifiedRecordService {

	
	public PlanItemModifiedRecordServiceImpl() {
		super(PlanItemModifiedRecord.class);
		
	}
	/**
	 * 
	 */
	private static final long serialVersionUID = -2905324156090618944L;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	@Transactional(readOnly=true)
	public Collection<PlanItemModifiedRecord> getsByHost(MonthPlanningItem mp)
			throws DataAccessException {
		if(mp==null) return Lists.newArrayList();
		String hql = String.format("from %s p where p.host=:host order by p.updateDate desc",table());
		List list = getSession().createQuery(hql).setParameter("host", mp).list();
		return list;
	}

	
	@Override
	@Transactional(readOnly=true)
	public Double calculateFinal(MonthPlanningItem mp)
			throws DataAccessException {
		Collection<PlanItemModifiedRecord> pms = getsByHost(mp);
		Double value = 0D;
		for(PlanItemModifiedRecord pr :pms){
			value+=pr.getValue();
		}
		return value;
	}


}
